﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IceScream.Models;
using IceScream.Common;

namespace IceScream.Controllers
{
    [Authorize(Users = "Admin")]
    public class AdminController : Controller
    {
        DataEntities storeDB = new DataEntities();

        public ActionResult Index()
        {
            var admin = storeDB.Admins.ToList();
            return View(admin);
        }

        //
        // GET: /Admin/Create

        public ActionResult Create()
        {
            var admin = new Admin();
            return View(admin);
        }

        //
        // POST: /Admin/Create

        [HttpPost]
        public ActionResult Create(Admin admin)
        {
            try
            {
                if (ModelState.IsValid && admin.Password.CompareTo(admin.RePassword) == 0)
                {
                    admin.Password = UICommon.Encryption(admin.Password);
                    storeDB.AddToAdmins(admin);
                    storeDB.SaveChanges();
                    return RedirectToAction("index");
                }
                ViewData["Error"] = "Kiểm tra lại password!";
                return View(admin);
            }
            catch
            {
                return View(admin);
            }
        }

        //
        // GET: /Admin/Edit/5

        public ActionResult Edit(int id)
        {
            Admin admin = storeDB.Admins.Where(s => s.Id == id).FirstOrDefault();            
            return View(admin);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        public ActionResult Edit(int id,Admin admin, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                var AdExist = storeDB.Admins.Where(s => s.Id == id).FirstOrDefault();
                if (ModelState.IsValid)
                {
                    if (!string.IsNullOrEmpty(admin.Password))
                    {
                        AdExist.Password = UICommon.Encryption(admin.Password);
                    }
                    AdExist.FirstName = admin.FirstName;
                    AdExist.LastName = admin.LastName;
                    AdExist.Roll = admin.Roll;

                    UpdateModel(AdExist, "Admins");
                    storeDB.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here
                Admin admin = storeDB.Admins.Where(s => s.Id == id).FirstOrDefault();
                storeDB.Admins.DeleteObject(admin);
                storeDB.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /Admin/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                Admin admin = storeDB.Admins.Where(s => s.Id == id).FirstOrDefault();
                storeDB.Admins.DeleteObject(admin);
                storeDB.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
